package test

import (
	"fmt"

	"gitee.com/tonyjia/gb28181/pkg/config"
	"gitee.com/tonyjia/gb28181/pkg/event"
	"gitee.com/tonyjia/gb28181/pkg/gbsip"
	"gitee.com/tonyjia/gb28181/pkg/model"
	"gitee.com/tonyjia/gb28181/pkg/model/constant"
	log "github.com/sirupsen/logrus"
)

var (
	cameraChannel = model.Channel{
		DeviceId: "34020001001320000001",
	}
	deviceCamera = model.Device{
		Name:     "",
		DeviceID: "34020001001320000001",
		Region:   "3402000100",
		Host:     "10.88.203.96",
		Port:     "5060",
	}
	deviceServer = model.Device{
		Name:     "lxj",
		DeviceID: "3402000100132000000",
		Region:   "3402000100",
		Host:     "10.88.204.168",
		Port:     "5060",
	}
	sipCommToServer = gbsip.SipCommander{
		SipInfo: model.SipServerInfo{
			Username: "lxj",
			ReginID:  "3402000100",
			DeviceID: "3402000100132000000",
			IP:       "10.88.204.168",
			Port:     5060,
			PassWord: "12345678",
		},
		SysInfo: model.SipDeviceInfo{
			DeviceID:   "3402000100132000000",
			DeviceType: "IPC",
		},
	}
	sipCommToCamera = gbsip.SipCommander{
		SipInfo: model.SipServerInfo{
			Username: "34020001001320000001",
			ReginID:  "3402000100",
			DeviceID: "34020001001320000001",
			IP:       "10.88.203.96",
			Port:     5060,
		},
		SysInfo: model.SipDeviceInfo{
			DeviceID:   "34020001001320000001",
			DeviceType: "IPC",
		},
	}
	mediaServer = model.MediaServer{
		IP:   "10.88.204.168",
		Port: 8088,
	}
)

// RegisterEventHandlers 注册事件处理函数测试,该函数用于接受notify后获得的通知
func RegisterEventHandlers() {
	eventBus := event.GetEventBus()

	eventBus.RegisterHandler(event.Catalog, CatalogEventHandler)
	eventBus.RegisterHandler(event.Keepalive, KeepaliveEventHandler)
}

func CatalogEventHandler(subscriberID string, data interface{}) {
	log.Infof("保活数据，%s： %v\n", subscriberID, data)
}

func KeepaliveEventHandler(subscriberID string, data interface{}) {
	log.Infof("保活数据，%s： %v\n", subscriberID, data)
}

func GetHandleMap() map[int]config.Process {
	handMap := map[int]config.Process{}
	handMap[constant.EVENT_MESSAGE_Query_Catalog] = func(data interface{}) (interface{}, error) {
		fmt.Println("查询设备通道")
		var channels []model.Channel
		channel := model.Channel{
			DeviceId:     "11111",
			Name:         "test",
			Manufacturer: "",
			Model:        "",
			Owner:        "",
			CivilCode:    "",
			Address:      "",
			Parental:     "",
			ParentID:     "",
			SafetyWay:    "",
			RegisterWay:  "",
			Secrecy:      "",
			Status:       "OK",
		}
		channels = append(channels, channel)
		return channels, nil
	}
	handMap[constant.EVENT_MESSAGE_Query_DeviceInfo] = func(data interface{}) (interface{}, error) {
		log.Info("回调查询设备信息")
		device := model.Device{
			DeviceID: "2222200001000001",
		}
		return device, nil
	}
	handMap[constant.EVENT_MESSAGE_DeviceInfo] = func(data interface{}) (interface{}, error) {
		//log.Infof("获取设备信息%+v", data)
		//device := data.(model.Device)
		//log.Infof("获取设备信息%+v", device)
		return nil, nil
	}
	handMap[constant.EVENT_MESSAGE_Catalog] = func(data interface{}) (interface{}, error) {
		//log.Infof("获取设备信息%+v", data)
		//device := data.([]model.Channel)
		//log.Infof("获取设备信息%+v", device)
		return nil, nil
	}
	handMap[constant.Event_NOTIFY_Keepalive] = func(data interface{}) (interface{}, error) {
		//log.Infof("获取保活通知信息：%+v", data)
		//notifyMessage := data.(model.Keepalive)
		//log.Infof("获取保活通知信息: %+v", notifyMessage)
		return nil, nil
	}
	handMap[constant.Event_NOTIFY_Catalog] = func(data interface{}) (interface{}, error) {
		//log.Infof("获取通道订阅信息：%+v", data)
		//notifyMessage := data.(model.DeviceCatalogResponse)
		//log.Infof("获取通道订阅信息: %+v", notifyMessage)
		return nil, nil
	}

	return handMap
}
